package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.dao.FamousTeacherDao;
import com.jinzhi.jzweb.domain.FamousTeacherDO;
import com.jinzhi.jzweb.pojo.FamousTeacherVO;
import com.jinzhi.jzweb.service.FamousTeacherService;
import com.jinzhi.sys.domain.UserDO;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.impl.regex.REUtil;
import org.springframework.stereotype.Service;

import java.util.List;


/***
 * @author C
 * @Date 2023/12/6
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class FamousTeacherServiceImpl extends CoreServiceImpl<FamousTeacherDao, FamousTeacherDO> implements FamousTeacherService {

    public static final int DELETED = 1;

    /**
     * 条件分页--Page<FamousTeacherDO>
     *
     * @param pageNum
     * @param pageSize
     * @param famousTeacherVO
     */
    @Override
    public Result getPage(int pageNum, int pageSize, FamousTeacherVO famousTeacherVO) {
        // 校验
        if (pageNum<=0 && pageSize<=0){
            pageNum=1;
            pageSize=10;
        }
        // 分页
        Page<FamousTeacherDO> page = new Page<>(pageNum,pageSize);
        // 根据姓名搜索
        EntityWrapper<FamousTeacherDO> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(famousTeacherVO.getName()) && !famousTeacherVO.getName().equals("")){
            wrapper.like("name",famousTeacherVO.getName())
                    .eq("is_deleted",0)
                    .orderBy(true,"level",false).orderBy("create_date",true);
        }else {
            wrapper.eq("is_deleted",0).orderBy(true,"level",false)
                    .orderBy("create_date",true);
        }
        // 判断职位信息的数量
        Page<FamousTeacherDO> selectPage = this.selectPage(page, wrapper);
        List<FamousTeacherDO> list = selectPage.getRecords();
        for (FamousTeacherDO famousTeacherDO : list) {
            if (famousTeacherDO.getPosition().contains(",") || famousTeacherDO.getPosition().contains("，")){
                String[] positionList = famousTeacherDO.getPosition().split(",");
                famousTeacherDO.setPositionList(positionList);
            }
        }
        // 校验查询是否存在空数据
        if (list.size()<=0){
            return Result.fail("未查询到名师信息 请更换姓名后重试");
        }

        return Result.ok(selectPage);

    }

    /**
     * 根据id查询
     *
     * @param id
     */
    @Override
    public FamousTeacherDO getById(Long id) {
        if (id<=0){
            throw new RuntimeException("参数异常");
        }

        EntityWrapper<FamousTeacherDO> wrapper = new EntityWrapper<>();
        wrapper.eq("id",id).eq("is_deleted",0);
        FamousTeacherDO famousTeacherDO = this.selectOne(wrapper);

        if (famousTeacherDO==null){
            return null;
        }
        // 判断职位信息的数量
        String position = famousTeacherDO.getPosition();
        if (position.contains(",") || position.contains("，")){
            String[] positionList = position.split(",");
            famousTeacherDO.setPositionList(positionList);
        }
        return famousTeacherDO;
    }

    /**
     * 新增
     *
     * @param famousTeacherVO
     */
    @Override
    public void insertFamousTeacher(FamousTeacherVO famousTeacherVO) {
        if (famousTeacherVO==null){
            throw new RuntimeException("参数异常");
        }

        FamousTeacherDO famousTeacherDO = new FamousTeacherDO();
        famousTeacherDO.setName(famousTeacherVO.getName());
        famousTeacherDO.setImage(famousTeacherVO.getImage());
        famousTeacherDO.setPosition(famousTeacherVO.getPosition());
        famousTeacherDO.setDescribe(famousTeacherVO.getDescribe());
        famousTeacherDO.setLevel(famousTeacherVO.getLevel());

        this.insert(famousTeacherDO);
    }

    /**
     * 修改
     *
     * @param famousTeacherVO
     */
    @Override
    public void updateFamousTeacher(FamousTeacherVO famousTeacherVO) {
        // 校验参数
        if (famousTeacherVO==null){
            throw new RuntimeException("参数异常");
        }
        FamousTeacherDO famousTeacherDO = new FamousTeacherDO();
        famousTeacherDO.setId(famousTeacherVO.getId());
        famousTeacherDO.setName(famousTeacherVO.getName());
        famousTeacherDO.setImage(famousTeacherVO.getImage());
        famousTeacherDO.setPosition(famousTeacherVO.getPosition());
        famousTeacherDO.setDescribe(famousTeacherVO.getDescribe());
        famousTeacherDO.setLevel(famousTeacherVO.getLevel());

        EntityWrapper<FamousTeacherDO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted",0).eq("id",famousTeacherDO.getId());

        this.update(famousTeacherDO,wrapper);

    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteFamousTeacher(Long id) {
        if (id<=0){
            throw new RuntimeException("参数异常");
        }

        FamousTeacherDO famousTeacherDO = this.selectById(id);

        if (famousTeacherDO==null){
            throw new RuntimeException("删除失败，不存在该数据");
        }
        famousTeacherDO.setDeleted(DELETED);

        this.updateById(famousTeacherDO);
    }


    /**
     * 查询所有名师列表
     *
     * @return
     */
    @Override
    public List<FamousTeacherDO> getList() {
        EntityWrapper<FamousTeacherDO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.orderBy("level",false).orderBy("create_date",true);
        List<FamousTeacherDO> famousTeacherDOList = this.selectList(wrapper);
        // 校验
        if (famousTeacherDOList.size()<=0 && famousTeacherDOList==null){
            return null;
        }
        return famousTeacherDOList;
    }
}
